#ifndef GLPLAYWIDGET_H
#define GLPLAYWIDGET_H
#include <QOpenGLWidget>
#include <QOpenGLShaderProgram>
#include <QOpenGLFunctions>
#include <QOpenGLTexture>
#include <QFile>
#include "media/CameraCapture.h"

#include <QTimer>

#define ATTRIB_VERTEX 3
#define ATTRIB_TEXTURE 4
class CPlayWidget:public QOpenGLWidget,protected QOpenGLFunctions,public Camera::CameraObserver
{
    Q_OBJECT
public slots:
    void OnUpdateFrame();
    void OnPaintData(const uint8_t *data,uint32_t len);
public:
    typedef enum{
        TYPE_YUV420P,
        TYPE_RGB32,
    }IMG_TYPE;
    CPlayWidget(QWidget* parent);
    ~CPlayWidget();
    void PlayOneFrame();
    int SetDataType(IMG_TYPE);
    int OnCameraData(uint8_t *dat, uint32_t size) override;
    int SetImgSize(uint32_t width,uint32_t );
protected:
    QTimer tm;
    void initializeGL() Q_DECL_OVERRIDE;
    void resizeGL(int w, int h) Q_DECL_OVERRIDE;
    void paintGL() Q_DECL_OVERRIDE;
private:
    IMG_TYPE mType;  // 目前只支持到RGB32,YUV420P
    GLuint textureUniformY; //y纹理数据位置
    GLuint textureUniformU; //u纹理数据位置
    GLuint textureUniformV; //v纹理数据位置
    GLuint textureUniformRGB; //RGB纹理位置


    GLuint textureUnifromRGB; //rgb32 的纹理位置

    GLuint id_rgb;
    GLuint id_y;
    GLuint id_u;
    GLuint id_v; //v纹理对象ID

    QOpenGLTexture* m_pTextureRGB;  //RGB 纹理是一整块的

    QOpenGLTexture* m_pTextureY;  //y纹理对象
    QOpenGLTexture* m_pTextureU;  //u纹理对象
    QOpenGLTexture* m_pTextureV;  //v纹理对象
    QOpenGLShader *m_pVSHader;  //顶点着色器程序对象
    QOpenGLShader *m_pFSHader;  //片段着色器对象
    QOpenGLShaderProgram *m_pShaderProgram; //着色器程序容器
    int m_nVideoW; //视频分辨率宽
    int m_nVideoH; //视频分辨率高
    unsigned char *m_pBufYuv420p;
    unsigned char* m_pBufRgb32;

    FILE* m_pYuvFile;

    void initShaderYuv();
    void initShaderRgb();

    int loadYuvTexture();
    int loadRgbTexture();
};
#endif
